// 导入模块
const api = require('../../api/api.js')

Page({
  data: {
    method:'',
    start:0,
    count:12,
    total:0,
    films:{},
    showNomore:false,
    showLoading:false
  },
  onLoad(options) {
    this.data.method = options.method
  },
  onReady(){
    this.loadListData()
  },
  loadListData(){
    return api[this.data.method]({
      start:this.data.start,
      count:this.data.count
    }).then(data => {
      let list = this.data.films.list || []
      let films = {
        title: data.subject_collection.name,
        list: list.concat(data.subject_collection_items) // 上拉加载时进行数据累加
      }
      this.setData({
        films: films,
        start:this.data.start+this.data.count, // 起始索引增加
        total:data.total
      })
      // 动态设置顶部标题
      wx.setNavigationBarTitle({
        title: data.subject_collection.name
      })
    })
  },
  // 监听下拉刷新
  onPullDownRefresh(){
    this.setData({
      start:0,
      films:{},
      showNomore:false
    })
    this.loadListData()
  },
  // 监听上拉加载更多
  onReachBottom(){
    // 判断是否还有更多数据
    if(this.data.start > this.data.total){
      return this.setData({
        showNomore: true // 提示没有更多数据了
      })
    }
    // 显示loding
    this.setData({
      showLoading:true
    })
    // 加载更多数据
    this.loadListData().then(() => {
      // 隐藏loading
      this.setData({
        showLoading:false
      })
    })

  }
})